package saucaca.fragment;

import java.util.ArrayList;

import org.json.JSONException;

import saucaca.Adapter.PlaylistAdapter;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.PlaylistObject;
import saucaca.Util.PreferenceUtils;
import saucaca.Util.TagName;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;


public class OnlinePlayListFragment extends Fragment {
	String TAG = OnlinePlayListFragment.class.getSimpleName();
	MainActivity mActivity;
	String strKeyList;
	PlaylistAdapter adapter;
	ListView mView_listView;
	private View mView_EmptyLoading = null;
	private ProgressBar progressBar;
	ImageView imgEmpty, imgError;
	TextView txtStatus;
	ArrayList<PlaylistObject> listPlaylist;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		getActivity().getActionBar().hide();
		View view = inflater.inflate(R.layout.playlist_online, container, false);
		findViewById(view);
		mActivity.myActionBar.setMenu(R.string.string_playlist_online, false, false);
		return view;
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if(!hidden)
			mActivity.myActionBar.setMenu(R.string.string_playlist_online, false, false);
	}
	private void findViewById(View view) {
		mView_listView = (ListView) view.findViewById(R.id.listViewPlaylist);
		mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		mView_listView.setEmptyView(mView_EmptyLoading);
		progressBar = (ProgressBar) mView_EmptyLoading.findViewById(R.id.empty_loading_progress);
		imgEmpty = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_NoItem);
		imgError = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_Error);
		txtStatus = (TextView) mView_EmptyLoading.findViewById(R.id.empty_loading_text);
		mView_listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mActivity.switchContent(OnlinePlayListSong.class.getName(), listPlaylist.get(position));
			}
		});
		new GetPlaylist().execute("");
	}

	class GetPlaylist extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... params) {
			String str = WServiceLib.playlist("", "", PreferenceUtils.getString(getActivity(), TagName.KeyPreference_user));
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null)
				try {
					listPlaylist = JsonReader.listPlaylist(result);
					if (listPlaylist.size() > 0) {
						adapter = new PlaylistAdapter(getActivity(), listPlaylist);
						mView_listView.setAdapter(adapter);
					} else {
						progressBar.setVisibility(View.GONE);
						imgEmpty.setVisibility(View.VISIBLE);
						txtStatus.setText("Chưa có Playlist");
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
		}
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mActivity = (MainActivity) getActivity();
	}

	@Override
	public void onStart() {
		super.onStart();

	}

}
